package com.gexin.controller.system.generalIntegral;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.Pagination;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.activity.*;
import com.gexin.model.product.Products;
import com.gexin.service.memberAcitivty.MemberRuleService;
import com.gexin.service.generalIntegralService.GeneralIntgralService;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import static com.gexin.common.utils.ArithUtil.percent;
import static com.gexin.common.utils.ArithUtil.percentt;
import java.util.Optional;


@ControllerBind(controllerKey = "/system/activity/generalIntegral", viewPath = "WEB-INF/system/promotionaward")
public class GeneralIntegralController extends SystemBaseController {

    private static final  GeneralIntgralService generalIntgralService = new GeneralIntgralService();

    private static final  MemberRuleService memberRuleService = new MemberRuleService();


    public void index() {
        if (isAjax()) {
            Pagination pagination = memberRuleService.integralList();
            renderJson(pagination);
            return;
        }
        render("promotionaward_list.html");
    }

    public void toInviteeRuleList() {
        render("invitee_rule_list.html");
    }

    public void invteeRuleList() {
        if (isAjax()) {
            Pagination rule = memberRuleService.findRule();
            renderJson(rule);
        }
    }


    public void checkDuplicateLevelExists(){
        Integer level = getParaToInt("level");
        InviteeRule inviteeRule = memberRuleService.checkDuplicateMemberLevel(level);
        if(null!=inviteeRule){
            renderJson(new ApiResponse(false, "该会员级别已经存在!"));
        }else{
            renderJson(new ApiResponse(true));
        }
        return;
    }

    public void insertInviteeRule() throws IOException {
        Integer level = getParaToInt("level");
        checkDuplicateLevelExists();
        Integer proportion = getParaToInt("proportion");
        Boolean flag = memberRuleService.insertGeneralIntegralProportionRule(level,proportion);
        renderJson(new ApiResponse(flag, flag ? "添加成功!" : "添加失败!"));
        return;
    }

    public void deleteInviteeRule() {
        String[] idList = getParaValues("id[]");
        Boolean flag = memberRuleService.deleteInviteeRule(idList);
        renderJson(new ApiResponse(false, false ? "删除成功!" : "删除失败!"));
        return;
    }

    public void updateMemberLevel() {
        Integer multiIncremetn = getParaToInt("multi_incremetn");
        Integer integralExchangeProductProportion = getParaToInt("integral_exchange_product_proportion");
        Integer integralExchangeCoinProportion = getParaToInt("integral_exchange_coin_proportion");
        Integer coinExchangeMoneyProportion = getParaToInt("coin_exchange_money_proportion");
        Integer reserveProportion = getParaToInt("reserve_proportion");
        Integer integralExchangeCoinRate = getParaToInt("integral_exchange_coin_rate");
        String standard1 = getPara("standard");
        Integer id = getParaToInt("id");
        BigDecimal standard= BigDecimal.valueOf(percentt(Double.parseDouble(standard1),1));
        MemberLevel memberLevel=new MemberLevel();
        memberLevel.setId(id);
        memberLevel.setMultiIncremetn(multiIncremetn);
        memberLevel.setIntegralExchangeProductProportion(integralExchangeProductProportion);
        memberLevel.setIntegralExchangeCoinProportion(integralExchangeCoinProportion);
        memberLevel.setCoinExchangeMoneyProportion(coinExchangeMoneyProportion);
        memberLevel.setReserveProportion(reserveProportion);
        memberLevel.setIntegralExchangeCoinRate(integralExchangeCoinRate);
        memberLevel.setStandard(standard);
        Boolean flag = memberLevel.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }



    public void toIntegralSpeedRule() {
        render("integral_speed_rule.html");
    }


    public void getGeneralIntegralSpeedList() {
        GeneralIntgralService generalIntgralService = new GeneralIntgralService();
        List<GeneralIntegralSpeed> generalIntegralSpeedList = generalIntgralService.getGeneralIntegralSpeedList();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(generalIntegralSpeedList.size());
        pagination.setData(generalIntegralSpeedList);
        pagination.setMsg("返回成功");
        renderJson(pagination);
     }

    public void deletegeneralIntegralSpeedListById() {
        String id = getPara("id");
        boolean flag = generalIntgralService.deleteGeneralIntegralSpeedById(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }

    public void updateGeneralIntegralSpeed() {
        Integer generalIntegralSpeedProportion = getParaToInt("generalIntegralSpeed");
        String id = getPara("id");
        GeneralIntegralSpeed generalIntegralSpeed = new GeneralIntegralSpeed();
        generalIntegralSpeed.setId(id);
//        generalIntegralSpeed.setProportion(generalIntegralSpeedProportion);
        boolean flag = generalIntegralSpeed.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }

    public void toPrefecture() {
        render("product_activity.html");
    }


    public void productActivity(){
        if (isAjax()) {
            List<MemberIntegralCoinRule> memberIntegralCoinRuleList = MemberIntegralCoinRule.dao.findAll();
            Pagination pagination = new Pagination();
            pagination.setCode(0);
            pagination.setCount(memberIntegralCoinRuleList.size());
            pagination.setData(memberIntegralCoinRuleList);
            pagination.setMsg("返回成功");
            renderJson(pagination);
        }
    }


    public void toMemberRuleList() {
        render("rate_percent_list.html");
    }


    public void getMemberRule(){
            List<MemberLevel> memberLevelFans = MemberLevel.dao.getMemberLevelFans();
            Pagination pagination = new Pagination();
            pagination.setCode(0);
            pagination.setCount(memberLevelFans.size());
            pagination.setData(memberLevelFans);
            pagination.setMsg("返回成功");
            renderJson(pagination);
     }

    public void insertMemberRule() throws IOException {
        Integer multi_incremetn = getParaToInt("multi_incremetn");
        Integer integral_exchange_coin_proportion = getParaToInt("integral_exchange_coin_proportion");
        Integer integral_exchange_product_proportion = getParaToInt("integral_exchange_product_proportion");
        Integer coin_exchange_money_proportion = getParaToInt("coin_exchange_money_proportion");
        Integer reserve_proportion = getParaToInt("reserve_proportion");
        Integer integral_exchange_coin_rate = getParaToInt("integral_exchange_coin_rate");
        Integer level = getParaToInt("level");
        String standard = getPara("standard");
        Boolean flag = MemberLevel.insertMemberLevel(multi_incremetn,
                integral_exchange_coin_proportion,
                integral_exchange_product_proportion,
                coin_exchange_money_proportion,
                reserve_proportion,
                integral_exchange_coin_rate,
                standard,
                level
        );
        renderJson(new ApiResponse(flag, flag ? "添加成功!" : "添加失败!"));
        return;
    }


    public void setMemberLevelRule(int multiply,int level){
        MemberLevel memberLevel = MemberLevel.dao.getMemberLevel(level);
        if(memberLevel==null) {
            return;
        }
        memberLevel.setMultiIncremetn(multiply);
        boolean flag = memberLevel.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }

    public ApiResponse insertBalance(int balance){
//      当前用户
        MemberBillSpeedIntegral memberBillSpeedIntegral = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId("111777");
        if(memberBillSpeedIntegral==null){
            memberBillSpeedIntegral = new MemberBillSpeedIntegral();
//          memberBillSpeedIntegral.setSpeedIntegralAndCoinAndBillCoupon("111777",0,0,0,0,0,0);
            memberBillSpeedIntegral.save();
        }
        String userId = memberBillSpeedIntegral.getUserId();
        MemberInfo memberInfoByUserId = MemberInfo.dao.getMemberInfoByUserId(userId);
        if(memberInfoByUserId==null){
            return new ApiResponse("没有找到该用户");
        }
//      获得所有级别
        int level = memberInfoByUserId.getLevel()==null?0:memberInfoByUserId.getLevel();
        MemberLevel memberLevel = MemberLevel.dao.getMemberLevel(level);
        int multiIncremetn = memberLevel.getMultiIncremetn()==null?0:memberLevel.getMultiIncremetn();
        int i = multiIncremetn * balance;
        memberBillSpeedIntegral.setSpeedIntegral(BigDecimal.valueOf(i));
        memberBillSpeedIntegral.update();
        ApiResponse apiResponse = new ApiResponse(true, "添加成功");
         return apiResponse;
//        memberLevel.getM
//        }
        ////      获得所有级别
//        List<MemberLevel> levelList = MemberLevel.dao.findAll();
////      级别中找到
//        List<MemberLevel> collect = levelList.stream().filter(level -> level != null && level.equals(MemberInfoEnum.SECOND_LEVEL.getCode()) ||
//                level.equals(MemberInfoEnum.THIRD_LEVEL.getCode())).collect(Collectors.toList());
//        collect.stream().filter()
//        for(int i=0;i<collect.size();i++){
//
//        }
        //app_store.html
    }
    public void toAppStore() {
        render("app_store.html");
    }


    public void getAppStore(){
        List<AppStore> appStoreList = AppStore.dao.getAppStore();
        System.out.println(appStoreList);
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(appStoreList.size());
        pagination.setData(appStoreList);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }
    public void deleteAppStore() {
        String id = getPara("id");
        boolean flag = AppStore.deleteAppStore(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }
    public void insertAppStore() throws IOException {
        String url = getPara("url");
        Integer type = getParaToInt("type");
        String desc = getPara("desc");
        Boolean flag = AppStore.insertAppStore(url,type,desc);
        renderJson(new ApiResponse(flag, flag ? "添加成功!" : "添加失败!"));
        return;
    }
    public void updateAppStore() {
        String url = getPara("url");
        Integer type = getParaToInt("type");
        String desc = getPara("desc");
        String id = getPara("id");
        AppStore appStore = new AppStore();
        appStore.setId(id);
        appStore.setUrl(url);
        appStore.setType(type);
        appStore.setDesc(desc);
//        generalIntegralSpeed.setProportion(generalIntegralSpeedProportion);
        boolean flag = appStore.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }

    //app_store_url.html
    public void toAppStoreUrl() {
        render("app_store_url.html");
    }

    public void getAppStoreUrl(){
//        List<AppStoreUrl> appStoreUrlList = AppStoreUrl.dao.getAppStoreUrl();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
//        pagination.setCount(appStoreUrlList.size());
//        pagination.setData(appStoreUrlList);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }

    public void deleteAppStoreUrl() {
        String id = getPara("id");
        boolean flag = AppStoreUrl.deleteAppStoreUrl(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }

    public void insertAppStoreUrl() throws IOException {
        String app_store_url = getPara("app_store_url");
        Integer type = getParaToInt("type");
        Boolean flag = AppStoreUrl.insertAppStoreUrl(app_store_url,type);
        renderJson(new ApiResponse(flag, flag ? "添加成功!" : "添加失败!"));
        return;
    }

    public void updateAppStoreUrl() {
        String app_store_url = getPara("app_store_url");
        Integer type = getParaToInt("type");
        String id = getPara("id");
        AppStoreUrl appStoreUrl = new AppStoreUrl();
        appStoreUrl.setId(id);
        appStoreUrl.setAppStoreUrl(app_store_url);
        appStoreUrl.setType(type);
//        generalIntegralSpeed.setProportion(generalIntegralSpeedProportion);
        boolean flag = appStoreUrl.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }

    //app_store_url.html
    public void toReverseMultipleRule() {
        render("reverse_multiple_rule.html");
    }

    public void getReverseMultipleRule(){
        List<ReverseMultipleRule> reverseMultipleRuleList = ReverseMultipleRule.dao.getReverseMultipleRule();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(reverseMultipleRuleList.size());
        pagination.setData(reverseMultipleRuleList);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }

    public void insertReverseMultipleRule(){
        String reverse_multiple_min= getPara("reverse_multiple_min");
        String reverse_multiple_max= getPara("reverse_multiple_max");
        String reverse_multiple_count_min= getPara("reverse_multiple_count_min");
        String reverse_multiple_count_max= getPara("reverse_multiple_count_max");
        Boolean flag=ReverseMultipleRule.insertReverseMultipleRule(reverse_multiple_min,reverse_multiple_max,reverse_multiple_count_min,reverse_multiple_count_max);
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }

    public void updateReverseMultipleRule() {
        String id=getPara("id");
        String reverse_multiple_min= getPara("reverse_multiple_min");
        String reverse_multiple_max= getPara("reverse_multiple_max");
        String reverse_multiple_count_min= getPara("reverse_multiple_count_min");
        String reverse_multiple_count_max= getPara("reverse_multiple_count_max");
        BigDecimal reverseMultipleMin= BigDecimal.valueOf(percent(Integer.parseInt(reverse_multiple_min),100));
        BigDecimal reverseMultipleMax= BigDecimal.valueOf(percent(Integer.parseInt(reverse_multiple_max),100));
        BigDecimal reverseMultipleCountMin= BigDecimal.valueOf(percentt(Double.parseDouble(reverse_multiple_count_min),1));
        BigDecimal reverseMultipleCountMax= BigDecimal.valueOf(percentt(Double.parseDouble(reverse_multiple_count_max),1));
        ReverseMultipleRule reverseMultipleRule = new ReverseMultipleRule();
        reverseMultipleRule.setId(id);
        reverseMultipleRule.setReverseMultipleMin(reverseMultipleMin);
        reverseMultipleRule.setReverseMultipleMax(reverseMultipleMax);
        reverseMultipleRule.setReverseMultipleCountMin(reverseMultipleCountMin);
        reverseMultipleRule.setReverseMultipleCountMax(reverseMultipleCountMax);
        boolean flag = reverseMultipleRule.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }

    public void deleteReverseMultipleRule() {
        String id = getPara("id");
        boolean flag = ReverseMultipleRule.deleteReverseMultipleRule(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }

    //general_integral_speed_rule.html
    public void toGeneralIntegralSpeedRule() {
        render("general_integral_speed_rule.html");
    }

    public void getGeneralIntegralSpeedRule(){
        List<GeneralIntegralSpeedRule> generalIntegralSpeedRuleList= GeneralIntegralSpeedRule.dao.getGeneralIntegralSpeedRule();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(generalIntegralSpeedRuleList.size());
        pagination.setData(generalIntegralSpeedRuleList);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }

    public void deleteGeneralIntegralSpeedRule() {
        String id = getPara("id");
        boolean flag = GeneralIntegralSpeedRule.deletGeneralIntegralSpeedRule(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }

    public void insertGeneralIntegralSpeedRule() throws IOException {
        Integer integralCountProportion=getParaToInt("integral_count_proportion");
        Integer integralUseProportion=getParaToInt("integral_use_proportion");
        Integer incrementProportion=getParaToInt("increment_proportion");
        Boolean flag = GeneralIntegralSpeedRule.insertGeneralIntegralSpeedRule(integralCountProportion,integralUseProportion,incrementProportion);
        renderJson(new ApiResponse(flag, flag ? "添加成功!" : "添加失败!"));
        return;
    }

    //group_rule.html
    public void toGroupRule() {
        render("group_rule.html");
    }

    public void getGroupRule(){
        List<GroupRule> groupRules= GroupRule.dao.getGroupRule();
        System.out.println(groupRules);
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(groupRules.size());
        pagination.setData(groupRules);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }

    public void deleteGroupRule() {
        String id = getPara("id");
        boolean flag = GroupRule.deletGroupRule(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }

    public void insertGroupRule() throws IOException {
        Integer numberOfPeople=getParaToInt("number_of_people");
        Integer multiple=getParaToInt("multiple");
        Integer level=getParaToInt("level");
        Integer balance=getParaToInt("balance");
        Integer type=getParaToInt("type");
        Integer number_of_people_max=getParaToInt("number_of_people_max");
        Boolean flag = GroupRule.insertGroupRule(numberOfPeople,multiple,level,balance,type,number_of_people_max);
        renderJson(new ApiResponse(flag, flag ? "添加成功!" : "添加失败!"));
        return;
    }
    //updateGroupRuleCopy.
    public void updateGroupRule() {
        Integer number_of_people = getParaToInt("number_of_people");
        String multiple = getPara("multiple");
        Integer level = getParaToInt("level");
        Integer type = getParaToInt("type");
        Integer balance = getParaToInt("balance");
        Integer number_of_people_max = getParaToInt("number_of_people_max");
        String id = getPara("id");
        GroupRule groupRule = new GroupRule();
        groupRule.setId(id);
        groupRule.setNumberOfPeople(number_of_people);
        groupRule.setType(type);
        groupRule.setLevel(level);
        groupRule.setBalance(balance);
        groupRule.setNumberOfPeopleMax(number_of_people_max);
//        generalIntegralSpeed.setProportion(generalIntegralSpeedProportion);
        boolean flag = groupRule.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }
//toMemberLevel
    /*public void toMemberLevel() {
        render("member_level.html");
    }

    public void getMemberLevel(){
        List<MemberLevel> memberLevelList= MemberLevel.dao.getGroupRule();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(memberLevelList.size());
        pagination.setData(memberLevelList);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }*/
    //Products.html

    public void toProducts() {
        render("Products.html");
    }

    public void getProducts(){
        List<Products> products= Products.dao.getProducts();
        Pagination pagination = new Pagination();
        pagination.setCode(0);
        pagination.setCount(products.size());
        pagination.setData(products);
        pagination.setMsg("返回成功");
        renderJson(pagination);
    }

    public void deleteProducts() {
        String id = getPara("id");
        boolean flag = Products.deletProducts(id);
        renderJson(new ApiResponse(flag, flag ? "删除成功!" : "删除失败!"));
        return;
    }

    public void updateProducts() {
        String magnification = getPara("magnification");
        Integer id = getParaToInt("id");
        BigDecimal reverseMagnification = BigDecimal.valueOf(percentt(Double.parseDouble(magnification), 1));
        Products products = new Products();
        products.setId(id);
        products.setMagnification(reverseMagnification);
        boolean flag = products.update();
        renderJson(new ApiResponse(flag, flag ? "更新成功!" : "更新失败!"));
        return;
    }
    @Before(Tx.class)
    public ApiResponse getQrCode(){
        try {
            List<AppStoreUrl> appStoreStrList = AppStoreUrl.dao.findAll();
            Optional<AppStoreUrl> first = appStoreStrList.stream().filter(appStoreUrl -> new Integer("1").equals
                    (appStoreUrl.getType())).findFirst();
            AppStoreUrl appStore = null;
            if (first.isPresent()) {
                appStore = first.get();
            }
        }catch (Exception e){
            return new ApiResponse(true,"生成失败","0");
        }
        return new ApiResponse(true,"生成成功","200");

    }
}

